<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="角色管理">
            <el-row style="height:60px">
              <el-button
                type="danger"
                icon="el-icon-plus"
                size="small"
                @click="showDialog=true"
              >新增角色
              </el-button>
            </el-row>
            <el-table v-loading="loading" border :data="list">
              <el-table-column
                label="序号"
                width="120"
                type="index"
              />
              <el-table-column
                label="角色描述"
                width="240"
                prop="name"
              />
              <el-table-column
                label="描述"
                prop="description"
              />
              <el-table-column
                label="操作"
              >
                <template slot-scope="scope">
                  <el-button type="success" size="small" @click="assignPerm(scope.row.id)">分配权限</el-button>
                  <el-button type="primary" size="small" @click="editRole(scope.row.id)">编辑</el-button>
                  <el-button type="danger" size="small" @click="delDetails(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyInfo.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-row type="flex" justify="center" align="middle" style="height:60px">
        <el-pagination
          :current-page.sync="page.page"
          :page-size.sync="page.pagesize"
          layout=" prev, pager, next"
          :total="page.total"
          @current-change="getRoleList"
        />
      </el-row>
      <!--编辑区域-->
      <el-dialog
        title="编辑弹层"
        :visible="showDialog"
        width="30%"
        @close="btnCancel"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 分配权限区域-->
      <el-dialog
        title="分配权限弹层"
        :visible="showPermDialog"
        width="30%"
        @close="btnPerson"
      >
        <el-tree
          ref="treeRef"
          :data="treeData"
          :props="{label:'name'}"
          show-checkbox
          default-expand-all
          :default-checked-keys="permIds"
          node-key="id"
          :check-strictly="true"
        />
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnPermCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnPermOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { assignPerm, delCompanyInfo, getCompanyInfo, getRoleList } from '@/api/setting'
import { mapGetters } from 'vuex'
import { addRole, getRoleDetail, updateRole } from '@/api/user'
import { getPermissionList } from '@/api/permisson'
import { transData } from '@/utils'

export default {
  data() {
    return {
      treeData: [],
      loading: false,
      permIds: [],
      currentRoleId: [],
      list: [],
      page: {
        page: 1,
        pagesize: 2,
        total: 0
      },
      companyInfo: {},
      roleForm: {},
      showDialog: false,
      showPermDialog: false,
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getRoleList()
    this.getCompanyDetails()
  },
  methods: {
    async editRole(id) {
      this.showDialog = true
      this.roleForm = await getRoleDetail(id)
    },
    async btnOK() {
      try {
        await this.$refs.roleForm.validate()
        // 只有校验通过的情况下 才会执行await的下方内容
        // roleForm这个对象有id就是编辑 没有id就是新增
        if (this.roleForm.id) {
          await updateRole(this.roleForm)
        } else {
          // 新增业务
          await addRole(this.roleForm)
        }
        // 重新拉取数据
        await this.getRoleList()
        this.$message.success('操作成功')
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    btnPerson() {
      this.showPermDialog = false
      this.permIds = []
    },
    async assignPerm(id) {
      this.treeData = transData(await getPermissionList(), '0')
      const { permIds } = await getRoleDetail(id)
      this.permIds = permIds
      this.currentRoleId = id
      console.log(id)
      this.showPermDialog = true
    },
    btnPermCancel() {
      this.showPermDialog = false
    },
    async btnPermOK() {
      console.log(this.currentRoleId)
      console.log(this.$refs.treeRef.getCheckedKeys())
      try {
        await assignPerm({
          permIds: this.$refs.treeRef.getCheckedKeys(),
          id: this.currentRoleId
        })
        this.$message.success('修改权限成功')
        this.btnPermCancel()
      } catch (e) {
        this.$message.error('修改失败')
      }
    },
    btnCancel() {
      this.roleForm = {
        name: '',
        description: ''
      }
      // 移除校验
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    async getCompanyDetails() {
      const res = await getCompanyInfo(this.companyId)
      this.companyInfo = res
    },
    async getRoleList() {
      const { rows, total } = await getRoleList(this.page)
      this.list = rows
      this.page.total = total
    },
    async delDetails(id) {
      // console.log(id)
      const length = this.list.length
      try {
        await this.$confirm('确认删除吗？')
        const res = await delCompanyInfo(id)
        console.log(res)
        this.$message.success('删除成功')
        if (length === 1 && this.page.page > 1) {
          this.page.page--
        }
        // 重新加载数据
        await this.getRoleList()
        this.loadig = true
      } catch (e) {
        this.$message.error('删除失败')
        console.log(e)
      }
    }
  }
}
</script>

<style>

</style>
